/*   1:    */ package org.intellij.images.editor.impl;
/*   2:    */ 
/*   3:    */

import com.intellij.ide.CopyPasteSupport;
import com.intellij.ide.DeleteProvider;
import com.intellij.ide.PsiActionSupportFactory;
import com.intellij.ide.PsiActionSupportFactory.PsiElementSelector;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiManager;
import com.intellij.ui.PopupHandler;
import com.intellij.ui.ScrollPaneFactory;
import com.intellij.ui.components.JBLayeredPane;
import com.intellij.util.ui.UIUtil;
import org.intellij.images.ImagesBundle;
import org.intellij.images.editor.ImageDocument;
import org.intellij.images.editor.ImageEditor;
import org.intellij.images.editor.ImageZoomModel;
import org.intellij.images.options.*;
import org.intellij.images.ui.ImageComponent;
import org.intellij.images.ui.ImageComponentDecorator;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;

/*   4:    */
/*   5:    */
/*   6:    */
/*   7:    */
/*   8:    */
/*   9:    */
/*  10:    */
/*  11:    */
/*  12:    */
/*  13:    */
/*  14:    */
/*  15:    */
/*  16:    */
/*  17:    */
/*  18:    */
/*  19:    */
/*  20:    */
/*  21:    */
/*  22:    */
/*  23:    */
/*  24:    */
/*  25:    */
/*  26:    */
/*  27:    */
/*  28:    */
/*  29:    */
/*  30:    */
/*  31:    */
/*  32:    */
/*  33:    */
/*  34:    */
/*  35:    */
/*  36:    */
/*  37:    */
/*  38:    */
/*  39:    */
/*  40:    */
/*  41:    */
/*  42:    */
/*  43:    */
/*  44:    */
/*  45:    */
/*  46:    */
/*  47:    */
/*  48:    */
/*  49:    */
/*  50:    */
/*  51:    */
/*  52:    */
/*  53:    */
/*  54:    */
/*  55:    */
/*  56:    */
/*  57:    */
/*  58:    */
/*  59:    */
/*  60:    */ 
/*  61:    */ final class ImageEditorUI
/*  62:    */   extends JPanel
/*  63:    */   implements DataProvider
/*  64:    */ {
/*  65:    */   @NonNls
/*  66:    */   private static final String a = "image";
/*  67:    */   @NonNls
/*  68:    */   private static final String b = "error";
/*  69:    */   private final ImageEditor c;
/*  70:    */   private final DeleteProvider d;
/*  71:    */   private final CopyPasteSupport e;
/*  72: 68 */   private final ImageZoomModel f = new ImageZoomModelImpl(null);
/*  73: 69 */   private final ImageWheelAdapter g = new ImageWheelAdapter(null);
/*  74: 70 */   private final ChangeListener h = new DocumentChangeListener(null);
/*  75: 71 */   private final ImageComponent i = new ImageComponent();
/*  76:    */   private final JPanel j;
/*  77:    */   private final JLabel k;
/*  78:    */   
/*  79:    */   ImageEditorUI(ImageEditor paramImageEditor, EditorOptions paramEditorOptions)
/*  80:    */   {
/*  81: 76 */     this.c = paramImageEditor;
/*  82: 77 */     PsiActionSupportFactory localPsiActionSupportFactory = PsiActionSupportFactory.getInstance();
/*  83: 78 */     if (localPsiActionSupportFactory != null) {
/*  84: 79 */       this.e = localPsiActionSupportFactory.createPsiBasedCopyPasteSupport(paramImageEditor.getProject(), this, new PsiActionSupportFactory.PsiElementSelector()
/*  85:    */       {
/*  86:    */         public PsiElement[] getSelectedElements()
/*  87:    */         {
/*  88: 82 */           PsiElement[] arrayOfPsiElement = (PsiElement[])LangDataKeys.PSI_ELEMENT_ARRAY.getData(ImageEditorUI.this);
/*  89: 83 */           return arrayOfPsiElement == null ? PsiElement.EMPTY_ARRAY : arrayOfPsiElement;
/*  90:    */         }
/*  91:    */       });
/*  92:    */     } else {
/*  93: 87 */       this.e = null;
/*  94:    */     }
/*  95: 90 */     this.d = (localPsiActionSupportFactory == null ? null : localPsiActionSupportFactory.createPsiBasedDeleteProvider());
/*  96:    */     
/*  97: 92 */     ImageDocument localImageDocument = this.i.getDocument();
/*  98: 93 */     localImageDocument.addChangeListener(this.h);
/*  99:    */     
/* 100:    */ 
/* 101: 96 */     TransparencyChessboardOptions localTransparencyChessboardOptions = paramEditorOptions.getTransparencyChessboardOptions();
/* 102: 97 */     GridOptions localGridOptions = paramEditorOptions.getGridOptions();
/* 103: 98 */     this.i.setTransparencyChessboardCellSize(localTransparencyChessboardOptions.getCellSize());
/* 104: 99 */     this.i.setTransparencyChessboardWhiteColor(localTransparencyChessboardOptions.getWhiteColor());
/* 105:100 */     this.i.setTransparencyChessboardBlankColor(localTransparencyChessboardOptions.getBlackColor());
/* 106:101 */     this.i.setGridLineZoomFactor(localGridOptions.getLineZoomFactor());
/* 107:102 */     this.i.setGridLineSpan(localGridOptions.getLineSpan());
/* 108:103 */     this.i.setGridLineColor(localGridOptions.getLineColor());
/* 109:    */     
/* 110:    */ 
/* 111:106 */     ImageContainerPane localImageContainerPane = new ImageContainerPane(this.i);
/* 112:107 */     localImageContainerPane.addMouseListener(new EditorMouseAdapter(null));
/* 113:108 */     localImageContainerPane.addMouseListener(new FocusRequester(null));
/* 114:    */     
/* 115:110 */     JScrollPane localJScrollPane = ScrollPaneFactory.createScrollPane(localImageContainerPane);
/* 116:111 */     localJScrollPane.setVerticalScrollBarPolicy(20);
/* 117:112 */     localJScrollPane.setHorizontalScrollBarPolicy(30);
/* 118:    */     
/* 119:    */ 
/* 120:115 */     localJScrollPane.addMouseWheelListener(this.g);
/* 121:    */     
/* 122:    */ 
/* 123:118 */     setLayout(new BorderLayout());
/* 124:    */     
/* 125:120 */     ActionManager localActionManager = ActionManager.getInstance();
/* 126:121 */     ActionGroup localActionGroup = (ActionGroup)localActionManager.getAction("Images.EditorToolbar");
/* 127:122 */     ActionToolbar localActionToolbar = localActionManager.createActionToolbar("Images.Editor", localActionGroup, true);
/* 128:    */     
/* 129:    */ 
/* 130:125 */     localActionToolbar.setTargetComponent(this);
/* 131:    */     
/* 132:127 */     JComponent localJComponent = localActionToolbar.getComponent();
/* 133:128 */     localJComponent.addMouseListener(new FocusRequester(null));
/* 134:    */     
/* 135:130 */     JLabel localJLabel = new JLabel(ImagesBundle.message("error.broken.image.file.format", new Object[0]), Messages.getErrorIcon(), 0);
/* 136:    */     
/* 137:    */ 
/* 138:    */ 
/* 139:    */ 
/* 140:135 */     JPanel localJPanel1 = new JPanel(new BorderLayout());
/* 141:136 */     localJPanel1.add(localJLabel, "Center");
/* 142:    */     
/* 143:138 */     this.j = new JPanel(new CardLayout());
/* 144:139 */     this.j.add(localJScrollPane, "image");
/* 145:140 */     this.j.add(localJPanel1, "error");
/* 146:    */     
/* 147:142 */     JPanel localJPanel2 = new JPanel(new BorderLayout());
/* 148:143 */     localJPanel2.add(localJComponent, "West");
/* 149:144 */     this.k = new JLabel((String)null, 4);
/* 150:145 */     localJPanel2.add(this.k, "East");
/* 151:    */     
/* 152:147 */     add(localJPanel2, "North");
/* 153:148 */     add(this.j, "Center");
/* 154:    */     
/* 155:150 */     a();
/* 156:    */   }
/* 157:    */   
/* 158:    */   private void a()
/* 159:    */   {
/* 160:154 */     ImageDocument localImageDocument = this.i.getDocument();
/* 161:155 */     BufferedImage localBufferedImage = localImageDocument.getValue();
/* 162:156 */     if (localBufferedImage != null)
/* 163:    */     {
/* 164:157 */       ColorModel localColorModel = localBufferedImage.getColorModel();
/* 165:158 */       String str = localImageDocument.getFormat();
/* 166:159 */       if (str == null) {
/* 167:160 */         str = ImagesBundle.message("unknown.format", new Object[0]);
/* 168:    */       } else {
/* 169:162 */         str = str.toUpperCase();
/* 170:    */       }
/* 171:164 */       VirtualFile localVirtualFile = this.c.getFile();
/* 172:165 */       this.k.setText(ImagesBundle.message("image.info", new Object[] { Integer.valueOf(localBufferedImage.getWidth()), Integer.valueOf(localBufferedImage.getHeight()), str, Integer.valueOf(localColorModel.getPixelSize()), localVirtualFile != null ? StringUtil.formatFileSize(localVirtualFile.getLength()) : "" }));
/* 173:    */     }
/* 174:    */     else
/* 175:    */     {
/* 176:170 */       this.k.setText(null);
/* 177:    */     }
/* 178:    */   }
/* 179:    */   
/* 180:    */   JComponent getContentComponent()
/* 181:    */   {
/* 182:176 */     return this.j;
/* 183:    */   }
/* 184:    */   
/* 185:    */   ImageComponent getImageComponent()
/* 186:    */   {
/* 187:180 */     return this.i;
/* 188:    */   }
/* 189:    */   
/* 190:    */   void dispose()
/* 191:    */   {
/* 192:184 */     this.i.removeMouseWheelListener(this.g);
/* 193:185 */     this.i.getDocument().removeChangeListener(this.h);
/* 194:    */     
/* 195:187 */     removeAll();
/* 196:    */   }
/* 197:    */   
/* 198:    */   ImageZoomModel getZoomModel()
/* 199:    */   {
/* 200:191 */     return this.f;
/* 201:    */   }
/* 202:    */   
/* 203:    */   private static final class ImageContainerPane
/* 204:    */     extends JBLayeredPane
/* 205:    */   {
/* 206:    */     private final ImageComponent a;
/* 207:    */     
/* 208:    */     public ImageContainerPane(ImageComponent paramImageComponent)
/* 209:    */     {
/* 210:198 */       this.a = paramImageComponent;
/* 211:199 */       add(paramImageComponent);
/* 212:    */     }
/* 213:    */     
/* 214:    */     private void a()
/* 215:    */     {
/* 216:203 */       Rectangle localRectangle = getBounds();
/* 217:204 */       Point localPoint = this.a.getLocation();
/* 218:205 */       localPoint.x = ((localRectangle.width - this.a.getWidth()) / 2);
/* 219:206 */       localPoint.y = ((localRectangle.height - this.a.getHeight()) / 2);
/* 220:207 */       this.a.setLocation(localPoint);
/* 221:    */     }
/* 222:    */     
/* 223:    */     public void invalidate()
/* 224:    */     {
/* 225:211 */       a();
/* 226:212 */       super.invalidate();
/* 227:    */     }
/* 228:    */     
/* 229:    */     public Dimension getPreferredSize()
/* 230:    */     {
/* 231:216 */       return this.a.getSize();
/* 232:    */     }
/* 233:    */     
/* 234:    */     protected void paintComponent(Graphics paramGraphics)
/* 235:    */     {
/* 236:221 */       super.paintComponent(paramGraphics);
/* 237:222 */       if (UIUtil.isUnderDarcula())
/* 238:    */       {
/* 239:223 */         paramGraphics.setColor(UIUtil.getControlColor().brighter());
/* 240:224 */         paramGraphics.fillRect(0, 0, getWidth(), getHeight());
/* 241:    */       }
/* 242:    */     }
/* 243:    */   }
/* 244:    */   
/* 245:    */   private final class ImageWheelAdapter
/* 246:    */     implements MouseWheelListener
/* 247:    */   {
/* 248:    */     private ImageWheelAdapter() {}
/* 249:    */     
/* 250:    */     public void mouseWheelMoved(MouseWheelEvent paramMouseWheelEvent)
/* 251:    */     {
/* 252:231 */       Options localOptions = OptionsManager.getInstance().getOptions();
/* 253:232 */       EditorOptions localEditorOptions = localOptions.getEditorOptions();
/* 254:233 */       ZoomOptions localZoomOptions = localEditorOptions.getZoomOptions();
/* 255:234 */       if ((localZoomOptions.isWheelZooming()) && (paramMouseWheelEvent.isControlDown()))
/* 256:    */       {
/* 257:235 */         if (paramMouseWheelEvent.getWheelRotation() < 0) {
/* 258:236 */           ImageEditorUI.this.f.zoomOut();
/* 259:    */         } else {
/* 260:238 */           ImageEditorUI.this.f.zoomIn();
/* 261:    */         }
/* 262:240 */         paramMouseWheelEvent.consume();
/* 263:    */       }
/* 264:    */     }
/* 265:    */   }
/* 266:    */   
/* 267:    */   private class ImageZoomModelImpl
/* 268:    */     implements ImageZoomModel
/* 269:    */   {
/* 270:246 */     private boolean a = false;
/* 271:    */     
/* 272:    */     private ImageZoomModelImpl() {}
/* 273:    */     
/* 274:    */     public double getZoomFactor()
/* 275:    */     {
/* 276:249 */       Dimension localDimension = ImageEditorUI.this.i.getCanvasSize();
/* 277:250 */       BufferedImage localBufferedImage = ImageEditorUI.this.i.getDocument().getValue();
/* 278:251 */       return localBufferedImage != null ? localDimension.getWidth() / localBufferedImage.getWidth() : 0.0D;
/* 279:    */     }
/* 280:    */     
/* 281:    */     public void setZoomFactor(double paramDouble)
/* 282:    */     {
/* 283:256 */       Dimension localDimension = ImageEditorUI.this.i.getCanvasSize();
/* 284:257 */       BufferedImage localBufferedImage = ImageEditorUI.this.i.getDocument().getValue();
/* 285:258 */       if (localBufferedImage != null)
/* 286:    */       {
/* 287:259 */         localDimension.setSize(localBufferedImage.getWidth() * paramDouble, localBufferedImage.getHeight() * paramDouble);
/* 288:260 */         ImageEditorUI.this.i.setCanvasSize(localDimension);
/* 289:    */       }
/* 290:263 */       ImageEditorUI.this.revalidate();
/* 291:264 */       ImageEditorUI.this.repaint();
/* 292:265 */       this.a = false;
/* 293:    */     }
/* 294:    */     
/* 295:    */     private double a()
/* 296:    */     {
/* 297:269 */       BufferedImage localBufferedImage = ImageEditorUI.this.i.getDocument().getValue();
/* 298:270 */       return localBufferedImage != null ? 1.0D / localBufferedImage.getWidth() : 0.0D;
/* 299:    */     }
/* 300:    */     
/* 301:    */     public void zoomOut()
/* 302:    */     {
/* 303:274 */       double d1 = getZoomFactor();
/* 304:275 */       if (d1 > 1.0D)
/* 305:    */       {
/* 306:277 */         setZoomFactor(d1 / 2.0D);
/* 307:    */       }
/* 308:    */       else
/* 309:    */       {
/* 310:280 */         double d2 = a();
/* 311:281 */         double d3 = (1.0D - d2) / 8.0D;
/* 312:282 */         int i = (int)Math.ceil((1.0D - d1) / d3);
/* 313:    */         
/* 314:284 */         setZoomFactor(1.0D - d3 * (i + 1));
/* 315:    */       }
/* 316:286 */       this.a = true;
/* 317:    */     }
/* 318:    */     
/* 319:    */     public void zoomIn()
/* 320:    */     {
/* 321:290 */       double d1 = getZoomFactor();
/* 322:291 */       if (d1 >= 1.0D)
/* 323:    */       {
/* 324:293 */         setZoomFactor(d1 * 2.0D);
/* 325:    */       }
/* 326:    */       else
/* 327:    */       {
/* 328:296 */         double d2 = a();
/* 329:297 */         double d3 = (1.0D - d2) / 8.0D;
/* 330:298 */         double d4 = (1.0D - d1) / d3;
/* 331:    */         
/* 332:300 */         setZoomFactor(1.0D - d3 * (d4 - 1.0D));
/* 333:    */       }
/* 334:302 */       this.a = true;
/* 335:    */     }
/* 336:    */     
/* 337:    */     public boolean canZoomOut()
/* 338:    */     {
/* 339:306 */       double d1 = getZoomFactor();
/* 340:307 */       double d2 = a();
/* 341:308 */       double d3 = (1.0D - d2) / 8.0D;
/* 342:309 */       double d4 = Math.ceil((1.0D - d1) / d3);
/* 343:    */       
/* 344:311 */       return d4 < 8.0D;
/* 345:    */     }
/* 346:    */     
/* 347:    */     public boolean canZoomIn()
/* 348:    */     {
/* 349:315 */       double d = getZoomFactor();
/* 350:316 */       return d < 32.0D;
/* 351:    */     }
/* 352:    */     
/* 353:    */     public boolean isZoomLevelChanged()
/* 354:    */     {
/* 355:320 */       return this.a;
/* 356:    */     }
/* 357:    */   }
/* 358:    */   
/* 359:    */   private class DocumentChangeListener
/* 360:    */     implements ChangeListener
/* 361:    */   {
/* 362:    */     private DocumentChangeListener() {}
/* 363:    */     
/* 364:    */     public void stateChanged(ChangeEvent paramChangeEvent)
/* 365:    */     {
/* 366:326 */       ImageDocument localImageDocument = ImageEditorUI.this.i.getDocument();
/* 367:327 */       BufferedImage localBufferedImage = localImageDocument.getValue();
/* 368:    */       
/* 369:329 */       CardLayout localCardLayout = (CardLayout)ImageEditorUI.this.j.getLayout();
/* 370:330 */       localCardLayout.show(ImageEditorUI.this.j, localBufferedImage != null ? "image" : "error");
/* 371:    */       
/* 372:332 */       ImageEditorUI.this.a();
/* 373:    */       
/* 374:334 */       ImageEditorUI.this.revalidate();
/* 375:335 */       ImageEditorUI.this.repaint();
/* 376:    */     }
/* 377:    */   }
/* 378:    */   
/* 379:    */   private class FocusRequester
/* 380:    */     extends MouseAdapter
/* 381:    */   {
/* 382:    */     private FocusRequester() {}
/* 383:    */     
/* 384:    */     public void mousePressed(MouseEvent paramMouseEvent)
/* 385:    */     {
/* 386:341 */       ImageEditorUI.this.requestFocus();
/* 387:    */     }
/* 388:    */   }
/* 389:    */   
/* 390:    */   private static final class EditorMouseAdapter
/* 391:    */     extends PopupHandler
/* 392:    */   {
/* 393:    */     public void invokePopup(Component paramComponent, int paramInt1, int paramInt2)
/* 394:    */     {
/* 395:349 */       ActionManager localActionManager = ActionManager.getInstance();
/* 396:350 */       ActionGroup localActionGroup = (ActionGroup)localActionManager.getAction("Images.EditorPopupMenu");
/* 397:351 */       ActionPopupMenu localActionPopupMenu = localActionManager.createActionPopupMenu("Images.Editor", localActionGroup);
/* 398:352 */       JPopupMenu localJPopupMenu = localActionPopupMenu.getComponent();
/* 399:353 */       localJPopupMenu.pack();
/* 400:354 */       localJPopupMenu.show(paramComponent, paramInt1, paramInt2);
/* 401:    */     }
/* 402:    */   }
/* 403:    */   
/* 404:    */   @Nullable
/* 405:    */   public Object getData(String paramString)
/* 406:    */   {
/* 407:362 */     if (CommonDataKeys.PROJECT.is(paramString)) {
/* 408:363 */       return this.c.getProject();
/* 409:    */     }
/* 410:364 */     if (CommonDataKeys.VIRTUAL_FILE.is(paramString)) {
/* 411:365 */       return this.c.getFile();
/* 412:    */     }
/* 413:366 */     if (CommonDataKeys.VIRTUAL_FILE_ARRAY.is(paramString)) {
/* 414:367 */       return new VirtualFile[] { this.c.getFile() };
/* 415:    */     }
/* 416:368 */     if (CommonDataKeys.PSI_FILE.is(paramString)) {
/* 417:369 */       return getData(CommonDataKeys.PSI_ELEMENT.getName());
/* 418:    */     }
/* 419:370 */     if (CommonDataKeys.PSI_ELEMENT.is(paramString))
/* 420:    */     {
/* 421:371 */       VirtualFile localVirtualFile = this.c.getFile();
/* 422:372 */       return (localVirtualFile != null) && (localVirtualFile.isValid()) ? PsiManager.getInstance(this.c.getProject()).findFile(localVirtualFile) : null;
/* 423:    */     }
/* 424:373 */     if (LangDataKeys.PSI_ELEMENT_ARRAY.is(paramString)) {
/* 425:374 */       return new PsiElement[] { (PsiElement)getData(CommonDataKeys.PSI_ELEMENT.getName()) };
/* 426:    */     }
/* 427:375 */     if ((PlatformDataKeys.COPY_PROVIDER.is(paramString)) && (this.e != null)) {
/* 428:376 */       return this.e.getCopyProvider();
/* 429:    */     }
/* 430:377 */     if ((PlatformDataKeys.CUT_PROVIDER.is(paramString)) && (this.e != null)) {
/* 431:378 */       return this.e.getCutProvider();
/* 432:    */     }
/* 433:379 */     if (PlatformDataKeys.DELETE_ELEMENT_PROVIDER.is(paramString)) {
/* 434:380 */       return this.d;
/* 435:    */     }
/* 436:381 */     if (ImageComponentDecorator.DATA_KEY.is(paramString)) {
/* 437:382 */       return this.c;
/* 438:    */     }
/* 439:385 */     return null;
/* 440:    */   }
/* 441:    */ }



/* Location:           E:\idea\

 * Qualified Name:     org.intellij.images.editor.impl.ImageEditorUI

 * JD-Core Version:    0.7.0.1

 */